package com.henry.utils.dynamic;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 峥桂 on 2017/5/26.
 */
public class ClassNameUtils {

    public static final String CONTROLLER           ="controller";
    public static final String ENTITY               ="entity";
    public static final String MAPPER               ="mapper";
    public static final String ENTITY_REQUEST_BEAN  ="entity_request_bean";
    public static final String SERVICE              ="service";
    public static final String SERVICEIMPL          ="serviceImpl";
    public static final String MAPPER_XML           ="mapper_xml";
    public static final String LIST_PAGE_JS         ="list_page_js";
    public static final String LIST_PAGE            ="list_page";
    public static final String ADD_PAGE             ="add_page";
    public static final String ADD_PAGE_JS          ="add_page_js";


    public static Map<String, String> dataTypeConvertMap = new HashMap<>();

    static {
        dataTypeConvertMap.put("tinyint", "Integer");
        dataTypeConvertMap.put("smallint", "Integer");
        dataTypeConvertMap.put("int", "Integer");
        dataTypeConvertMap.put("mediumint", "Integer");
        dataTypeConvertMap.put("integer", "Integer");
        dataTypeConvertMap.put("bigint", "Long");
        dataTypeConvertMap.put("float", "Float");
        dataTypeConvertMap.put("double", "Double");
        dataTypeConvertMap.put("decimal", "BigDecimal");
        dataTypeConvertMap.put("char", "String");
        dataTypeConvertMap.put("varchar", "String");
        dataTypeConvertMap.put("tinytext", "String");
        dataTypeConvertMap.put("text", "String");
        dataTypeConvertMap.put("mediumtext", "String");
        dataTypeConvertMap.put("longtext", "String");
        dataTypeConvertMap.put("date", "Date");
        dataTypeConvertMap.put("datetime", "Date");
        dataTypeConvertMap.put("timestamp", "Date");
    }

    public static int getColunmLength(String columnTypeStr){
        try {
            String length= columnTypeStr.substring(columnTypeStr.indexOf("(")+1,columnTypeStr.lastIndexOf(")"));
            return Integer.parseInt(length);
        }catch (Exception e) {
            return 0;
        }
    }

    public static File buildContentByFile(String template_path, String tempFileName, String templaterContent) {
        File file = null;
        try {
            File dict = new File(template_path);
            if(!dict.exists()){
                dict.mkdirs();
            }
            tempFileName=(StringUtils.isBlank(tempFileName)?"defaultVm.vm":tempFileName).trim();
            file = new File(template_path + "/" + tempFileName);
            FileUtils.writeStringToFile(file,templaterContent,"utf-8");
//            if (file.exists() && file.isFile()) {
//                file.delete();
//            }
//            file.createNewFile();
//            PrintWriter tempWrite = new PrintWriter(new FileWriter(file, false));
//            tempWrite.write(templaterContent);
//            tempWrite.flush();
//            tempWrite.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    /**
     * 获取文件名
     */
    public static String getFileName(String template, String className, String packageName, String customerName) {
       return getFileName(template, className, packageName,
               customerName,"","");
    }
 /**
     * 获取文件名
     */
    public static String getFileName(String template, String className, String packageName,
                                     String customerName,String viewParent,String viewName) {
        String packagePath = "";
        if (StringUtils.isNotBlank(packageName)) {
            packagePath = packageName.replace(".", File.separator) + File.separator;
        }
        if (StringUtils.isNotBlank(customerName)) {
            customerName = File.separator + customerName;
        }else{
            //customerName=File.separator+className.toLowerCase();
        }

        if(StringUtils.isBlank(viewParent)){
            viewParent="WEB-INF";
        }
        if(StringUtils.isBlank(viewName)){
            viewName="view";
        }
        template=template.toLowerCase();
        if (template.contains("entity.java.vm")) {
            return packagePath + "dao" + File.separator +"entity" + customerName + File.separator + className + ".java";
        }

        if (template.contains("mapper.java.vm")) {
            return packagePath + "dao" + File.separator  +"mapper"+ customerName + File.separator + className + "Mapper.java";
        }

        if (template.contains("mapper.xml.vm")) {
            return packagePath + "dao" + File.separator  +"mapper"+ customerName+ File.separator + className + "Mapper.xml";
        }

        if (template.contains("service.java.vm")) {
            return packagePath + "service" + customerName + File.separator + className + "Service.java";
        }

        if (template.contains("serviceimpl.java.vm")) {
            return packagePath + "service" + customerName + File.separator + "impl" + File.separator + className + "ServiceImpl.java";
        }

        if (template.contains("controller.java.vm")) {
            return packagePath + "web" + customerName + File.separator + className + "Controller.java";
        }
        if (template.contains("requestbean.java.vm")) {
            return packagePath + "web/vo" + customerName + File.separator + className + "RequestBean.java";
        }

        if (template.contains("list.ftl.vm")) {
            return viewParent+File.separator+viewName + customerName + File.separator + className.toLowerCase() + "List.ftl";
        }

        if (template.contains("list.js.vm")) {
            return viewParent+File.separator+"myjs" + customerName + File.separator + className.toLowerCase() + "List.js";
        }

        if (template.contains("add.ftl.vm")) {
            return viewParent+File.separator+viewName + customerName + File.separator + className.toLowerCase() + "_add.ftl";
        }

        if (template.contains("add.js.vm")) {
            return viewParent+File.separator+"myjs" + customerName + File.separator + className.toLowerCase() + "_add.js";
        }

        return null;
    }

    /**
     * 列名转换成Java属性名
     */
    public static String columnToJava(String columnName) {
        return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "");
    }

    /**
     * 表名转换成Java类名
     */
    public static String tableToJava(String tableName, String tablePrefix) {
        if (StringUtils.isNotBlank(tablePrefix)) {
            tableName = tableName.replace(tablePrefix, "");
        }
        return columnToJava(tableName);
    }

    public static String tableToJava(String tableName, String tablePrefixs[]) {
        if (tablePrefixs != null && tablePrefixs.length > 0) {
            for (String tablePrefix : tablePrefixs) {
                tableName = tableName.replace(tablePrefix, "");
            }
        }
        return columnToJava(tableName);
    }




}
